package com.iflytek.application.common.springdata.jdbc.repository.utils;

import org.springframework.util.StringUtils;

import java.util.UUID;


public class SqlUtils {

	public static String getAnalyseSQLOfcount(String sourceSQL) {
		String strSQL = "";
		sourceSQL = sourceSQL.replaceAll("FROM", "from");
		sourceSQL = sourceSQL.replaceAll("ORDER", "order");
		strSQL = "select   count(*)   as   count ";
		strSQL += sourceSQL.substring(sourceSQL.indexOf("from"));
		if (strSQL.indexOf("order") != -1) {
			strSQL = strSQL.substring(0, strSQL.indexOf("order"));
		}
		return strSQL;
	}
	
	/**
	 * @param metaName ID
	 * @param ids '1,3,5,9,11'
	 * @return 
	 */
	public String getInSql(String metaName,String ids){
		StringBuilder sql = new StringBuilder();
		if(!StringUtils.isEmpty(ids)){
			String[] idArr = ids.split(",");
			sql.append(" and "+metaName+" in(");
			for(int i=0;i<idArr.length;i++){
				sql.append("'"+idArr[i]+"'");
				if(i!=(idArr.length-1)){
					sql.append(",");
				}
			}
			sql.append(")");
		}
		return sql.toString();
	}
	
	public static String getUUID(){
		return UUID.randomUUID().toString().replace("-", "");
	}
}
